package com.smart.persistence.entity;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name = "T_RoomModel")
@NamedQueries({
		@NamedQuery(name = RoomModel.findMaxByIdHotel, query = "SELECT entity FROM RoomModel entity where entity.id = (SELECT MAX(room.id) FROM RoomModel room where room.hotel.id = ?1) "),
		@NamedQuery(name = RoomModel.findByCodeRoom, query = "SELECT entity FROM RoomModel entity where entity.codRoom = ?1 "),
		@NamedQuery(name = RoomModel.findAvailableInPeriodByCodHotel, query = "SELECT entity, count(rooms.id) FROM RoomModel entity left join entity.rooms as rooms left join rooms.booksRoom as books where entity.hotel.codHotel = ?1 and (rooms.booksRoom is empty or ( (books.checkIn <= ?2 and books.checkOut <= ?2) or (books.checkIn >= ?3 and books.checkOut >= ?3))) group by entity.id")

})
public class RoomModel {

	public final static String findAvailableInPeriodByCodHotel = "RoomModel.findAvailableInPeriodByCodHotel";
	public final static String findMaxByIdHotel = "RoomModel.findMaxByIdHotel";
	public final static String findByCodeRoom = "RoomModel.findByCodeRoom";

	@Id
	@Column(name = "id_room_type")
	@TableGenerator(name = "ROOM_TYPE_GEN", allocationSize = 1, initialValue = 1, table = "T_ID", pkColumnName = "key", valueColumnName = "valor", pkColumnValue = "ID_ROOM_TYPE")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "ROOM_TYPE_GEN")
	private int id;

	@Column(length = 55)
	private String name;

	private BigDecimal price;

	@Column(unique = true)
	private int codRoom;

	@Column(length = 300)
	private String description;
	@Column(length = 300)
	private String image;
	@Column(length = 300)
	private String info;
	@Column(length = 500)
	private String details;

	private int maxAmountAdults;

	private int maxAmountChildren;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_hotel", nullable = false)
	private Hotel hotel;

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "roomType")
	private List<Room> rooms;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public Hotel getHotel() {
		return hotel;
	}

	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}

	public int getMaxAmountAdults() {
		return maxAmountAdults;
	}

	public void setMaxAmountAdults(int maxAmountAdults) {
		this.maxAmountAdults = maxAmountAdults;
	}

	public int getMaxAmountChildren() {
		return maxAmountChildren;
	}

	public void setMaxAmountChildren(int maxAmountChildren) {
		this.maxAmountChildren = maxAmountChildren;
	}

	public int getCodRoom() {
		return codRoom;
	}

	public void setCodRoom(int codRoom) {
		this.codRoom = codRoom;
	}

	@Override
	public String toString() {
		return "RoomModel [id=" + id + ", name=" + name + ", price=" + price
				+ ", codRoom=" + codRoom + ", description=" + description
				+ ", image=" + image + ", info=" + info + ", details="
				+ details + ", maxAmountAdults=" + maxAmountAdults
				+ ", maxAmountChildren=" + maxAmountChildren + "]";
	}

}
